/**
 * @(#) AnnuitySubtractionProxy.as
 *
 * @date		2009. 04. 23.
 * @version		1.0
 * @author		Jo,Chang Hun
 * @description	 연말정산  - Proxy
 * 				 연금보험료 공제
 */
package com.skt.common_hr.yearadjustment.model
{
	import com.skt.common_hr.common.PsHrAlert;
	import com.skt.common_hr.common.puremvc.CommonProxy;
	import com.skt.common_hr.yearadjustment.YearAdjustmentFacade;
	import com.skt.common_hr.yearadjustment.model.vo.AnnuitySubtractionVO;
	import com.wcl.utils.DateUtil;
	
	import mx.collections.ArrayCollection;
	import mx.rpc.events.FaultEvent;
	import mx.rpc.events.ResultEvent;
	

	public class AnnuitySubtractionProxy extends CommonProxy
	{
		public static const NAME:String = "AnnuitySubtractionProxy";
		public var msgStr:String;
		
		public var annuityData:ArrayCollection = new ArrayCollection();
		public var annuityTypeList:ArrayCollection = new ArrayCollection();

		/**
		 * 생성자
		 * 
		 * @param
		 * @return void
		 */
		public function AnnuitySubtractionProxy(data:Object=null)
		{
			super(NAME, data);
		}
		
		
		/**
		 * data load를 수행
		 * 		- DataLoader 를 생성하고 Import Parameter 객체를 생성, RFC를 호출
		 * 
		 * @param pernr 사번
		 * @return void
		 */
		public function load(pernr:String, year:String):void
		{
			var param:Object = new Object();
			param["I_YEAR"] = year;
			param["I_PERNR"] = pernr;
			param["I_YEAGBN"] = "E1";		//E1: 연금보험료
			param["I_PROC_TYPE"] = "D";		//D:조회, S:저장
			loadData("Z_HR_0085_NEW", param, result, fault);
		}
		
		
		
		public function save(parnr:String, year:String, tableArr:Array):void{
			var param:Object = new Object();
			param["I_YEAR"] = year;
			param["I_PERNR"] = parnr;
			param["I_YEAGBN"] = "E1";		//E1: 연금보험료
			param["I_PROC_TYPE"] = "S";		//D:조회, S:저장
			var tables:Object = new Object();
			tables["ITAB3"] = tableArr;
			saveData("Z_HR_0085_NEW", param, tables, saveResult, saveFault); 
		}
		
		public function loadAnnuityType(year:String):void
		{
			var param:Object = new Object();
			param["I_YEAR"] = year;
			//param["I_INFTY"] = "0881";
			param["I_SUBTY"] = "E1";		//E1: 연금보험료
			loadData("Z_HR_0304", param, resultAnnuityType, faultAnnuityType);
		}

		///////////////////////////////////////////////////////////////////////////////////////////////////
		//
		//	Event Listeners
		//
		///////////////////////////////////////////////////////////////////////////////////////////////////
		/**
		 * 서버에서 정상적으로 리턴된 결과를 처리
		 * 
		 * @param
		 * @return void
		 */
		public function result(event:ResultEvent):void
		{
			if(retCode==0) {  
				trace( "suss to load donation proxy");
				annuityData = parseAnnuity(event.result.ITAB3);
				facade.sendNotification(YearAdjustmentFacade.LOAD_ANNUITY_SUBTRATION_COMPLETE, this);
			}
			else {
				trace( "failed to load donation: (" +event.result["RETCODE"] + ") "+event.result["RETTEXT"]);
				msgStr = event.result["RETTEXT"];
				facade.sendNotification(YearAdjustmentFacade.LOAD_ANNUITY_SUBTRATION_FAILED, this);
			}
		}
		
		/**
		 * 서버에서 오류가 발생했을 처리
		 * 
		 * @param
		 * @return void
		 */
		private function fault(event:FaultEvent):void
		{
			trace("Fault message in totalIncom: " + event.fault.faultString);
			msgStr = event.fault.faultString;
			facade.sendNotification(YearAdjustmentFacade.LOAD_DONATION_LIST_FAILED, this);
		}
		
		//저장 완료
		public function saveResult(event:ResultEvent):void
		{
			if(retCode==0) {  
				trace( "suss to save donation proxy");
				facade.sendNotification(YearAdjustmentFacade.SAVE_ANNUITY_SUBTRATION_COMPLETE, this);
			}
			else {
				trace( "failed to save donation: (" +event.result["RETCODE"] + ") "+event.result["RETTEXT"]);
				msgStr = event.result["RETTEXT"];
				facade.sendNotification(YearAdjustmentFacade.SAVE_ANNUITY_SUBTRATION_FAILED, this);
			}
		}
		
		//저장실패
		private function saveFault(event:FaultEvent):void
		{
			trace("Fault message in totalIncom: " + event.fault.faultString);
			msgStr = event.fault.faultString;
			facade.sendNotification(YearAdjustmentFacade.SAVE_ANNUITY_SUBTRATION_FAILED, this);
		}
		
		
		public function resultAnnuityType(event:ResultEvent):void
		{
			if(retCode==0) {  
				annuityTypeList = parseAnnuityTypeList(event.result.ITAB);
				facade.sendNotification(YearAdjustmentFacade.LOAD_ANNUITY_TYPE_LIST_COMPLETE, this);
			}
			else {
				trace( "failed to load donation: (" +event.result["RETCODE"] + ") "+event.result["RETTEXT"]);
				msgStr = event.result["RETTEXT"];
				facade.sendNotification(YearAdjustmentFacade.LOAD_ANNUITY_TYPE_LIST_FAILED, this);
			}
		}
		
		//저장실패
		private function faultAnnuityType(event:FaultEvent):void
		{
			msgStr = event.fault.faultString;
			facade.sendNotification(YearAdjustmentFacade.LOAD_ANNUITY_TYPE_LIST_FAILED, this);
		}
		
		///////////////////////////////////////////////////////////////////////////////////////////////////
		//
		//	Data Parsing functions
		//
		///////////////////////////////////////////////////////////////////////////////////////////////////
		/**
		 * 서버에서 전달된 result 객체를 분석하여 Value Object를 생성
		 * 
		 * @param
		 * @return 연말정산  MainList Value Object
		 */
		/**
		 * 개인정보
		 * 
		 * @param
		 * @return 개인정보 Value Object
		 */
		private function parseAnnuity(arr:ArrayCollection):ArrayCollection
		{
			var returnArr:ArrayCollection = new ArrayCollection();
			for each(var rec:Object in arr){
				var vo:AnnuitySubtractionVO = new AnnuitySubtractionVO();
				vo.PREIN	= rec["PREIN"];		//종(전)근무지
				vo.PNSTY	= rec["PNSTY"];		//
				vo.PNSTT	= rec["PNSTT"];		//
				vo.BETRG	= rec["BETRG"];		//금액
				vo.FINCO	= rec["FINCO"];		//금융기관코드
				vo.INSNM	= rec["INSNM"];		//연금저축 금융사명
				vo.ACCNO	= rec["ACCNO"];		//계좌번호
				vo.BEGDA	= DateUtil.stringToDateTime(rec["BEGDA"]);	//시작일
				vo.ENDDA	= DateUtil.stringToDateTime(rec["ENDDA"]);	//종료일
				
				returnArr.addItem(vo);
			}
			
			return returnArr;
		}
		
		private function parseAnnuityTypeList(arr:ArrayCollection):ArrayCollection
		{
			var returnArr:ArrayCollection = new ArrayCollection();
			for each(var reg:Object in arr){
				var vo:Object = new Object();
				
				vo.CODE	= reg["PNSTY"];
				vo.TEXT	= reg["PNSTX"];
				
				returnArr.addItem(vo);
			}
			returnArr.addItemAt({CODE:'',TEXT:'유형선택'}, 0);
			return returnArr;
		}
	}
}